﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using DigitalGenetics;

namespace SMA
{
    public partial class frmSMA : Form
    {
        public List<Metrics> ListMetrics { get; set; }
        public int schale = 1; // 1 dot per value

        public frmSMA()
        {
            InitializeComponent();

            string[] args = Environment.GetCommandLineArgs();
            if (args.Length > 1)
            {
                if (File.Exists(args[1]))
                {
                    String ext = Path.GetExtension(args[1]);
                    if(ext.Contains(".f")){
                        txtFastaFile.Text = args[1];
                    } else if(ext.Contains(".sm")){
                        txtSmFile.Text = args[1];
                    }
                }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            //panelSm.Invalidate();

            ListMetrics = new List<Metrics>();
        }

        private void SetHexFileName()
        {
            txtHexFile.Text = txtFastaFile.Text + ".hex";
            SetSmFileName();
        }

        private void SetSmFileName()
        {
            txtSmFile.Text = txtHexFile.Text + ".sm";
        }

        private void panelSm_Paint(object sender, PaintEventArgs e)
        {
            if (!File.Exists(txtSmFile.Text))
            {
                return;
            }

            Graphics g = e.Graphics;

            int level = 8;
            if (txtSmFile.Text.EndsWith(".sm1"))
            { level = 1; }
            SumMatrix sm = new SumMatrix(txtSmFile.Text, level); // int.Parse(lblLevel.Text));
            for (int y = 0; y < 256; y++)
            {
                for (int x = 0; x < 256; x++)
                {
                    int greys = sm.greys;
                    int a = 255 - sm.NormalizedSumMatrix[x, y] * 255 / (greys - 1);
                    Brush brush = SumMatrix.GreyBrushArray[a];// GetSmBrush(a);
                    g.FillRectangle(brush, (int)(x * schale), (int)(y * schale), (int)schale, (int)schale);
                }
            }
            
        }

        private void txtFastaFile_TextChanged(object sender, EventArgs e)
        {
            SetHexFileName();
            //txtFastaFile.Select(txtHexFile.Text.Length, 1);
        }

        private void txtHexFile_TextChanged(object sender, EventArgs e)
        {
            SetSmFileName();
            //txtHexFile.Select(txtHexFile.Text.Length, 1);
        }

        private void cmdFasta2Hex_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txtFastaFile.Text))
            {
                MessageBox.Show("File not found");
                return;
            }
            simpleProgressBarFasta.Visible = true;
            simpleProgressBarFasta.Location = txtHexFile.Location;
            this.Cursor = Cursors.WaitCursor;
            SumMatrix sm = new SumMatrix();
            sm.ReadLines += new SumMatrix.ReadLinesHandler(sm_ReadLines);
            sm.Open(txtFastaFile.Text, txtHexFile.Text, txtSmFile.Text);
            this.Cursor = Cursors.Default;
            simpleProgressBarFasta.Visible = false;
            simpleProgressBarFasta.Value = 0;
            simpleProgressBarFasta.Width = txtHexFile.Width;

            ShowMetrics(sm);

        }

        private void ShowMetrics(SumMatrix sm)
        {
            Metrics metrics = new Metrics();
            sm.GetMetrics(metrics);

            ListMetrics.Add(metrics);
            metricsBindingSource.DataSource = null;
            metricsBindingSource.DataSource = ListMetrics;

        }

        void sm_ReadLines(SumMatrix sm, int readLines)
        {
            simpleProgressBarFasta.Increment();
            toolStripStatusLabel1.Text = string.Format("Line {0}", readLines);
            statusStrip1.Refresh();
        }

        private void cmdShowSm_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txtSmFile.Text))
            {
                MessageBox.Show("File not found");
                return;
            }
            SumMatrix sm = new SumMatrix(txtSmFile.Text);
            ShowMetrics(sm);
        }

        private void ShowOpenDialog(string fileName, TextBox txtBox)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = fileName + "|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            DialogResult res = openFileDialog1.ShowDialog();
            if (res == DialogResult.OK)
            {
                txtBox.Text = openFileDialog1.FileName;
            }
        }

        private void cmdHex2Sm_Click(object sender, EventArgs e)
        {

        }

        private void simpleProgressBarFasta_Load(object sender, EventArgs e)
        {
            //simpleProgressBarFasta.Load += new EventHandler(simpleProgressBarFasta.SimpleProgressBar_Load);
            simpleProgressBarFasta.thisWidth = txtHexFile.Width;
        }

        private void cmdAddToSmStat_Click(object sender, EventArgs e)
        {
            string smStatFileName = Path.GetFullPath(txtSmStat.Text);
            if (File.Exists(smStatFileName))
            {
                MessageBox.Show(string.Format("File '{0}' will be appended", smStatFileName));
            }
            else
            {
                MessageBox.Show(string.Format("File '{0}' will be created", smStatFileName));
            }
        }

        private void cmdOpenFasta_Click(object sender, EventArgs e)
        {
            ShowOpenDialog("FASTA Files (*.fa, *.fna, *.fasta)|*.fa;*.fna;*.fasta", txtFastaFile);
        }

        private void cmdOpenHex_Click(object sender, EventArgs e)
        {
            ShowOpenDialog("Hex Files (*.hex)|*.hex;", txtHexFile);
        }

        private void cmdOpenSm_Click(object sender, EventArgs e)
        {
            ShowOpenDialog("SumMartix Files (*.sm, *.sm1, *.sm8)|*.sm;*.sm1;*.sm8", txtSmFile);
        }

        private void txtSmFile_TextChanged(object sender, EventArgs e)
        {
            panelSm.Invalidate();
        }

    }
}
